package org.icec.web.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.beetl.sql.core.engine.PageQuery;
import org.icec.web.shiro.annotation.CurrentUser;
import org.icec.web.sys.model.SysClient;
import org.icec.web.sys.model.SysClientAccount;
import org.icec.web.sys.model.SysUser;
import org.icec.web.sys.service.SysClientAccountService;
import org.icec.web.sys.service.SysClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/12/18.
 */
@Controller
@RequestMapping("/sys/client")
public class SysClientCtrl {
    @Autowired
    private SysClientService sysClientService;
    @Autowired
    private SysClientAccountService sysClientAccountService;
    @Value("${icec.upload.path}")
    private String filePath;

    @RequestMapping("list")
    public String list() {
            return "sys/client/clientList";
    }

    @RequestMapping("listGx")
    public String listGx() {
            return "sys/client/clientListGx";
    }


    /**
     * 进入添加界面
     * @return
     */
    @RequestMapping("add")
    @RequiresPermissions({"client:edit"})
    public String add() {
        return "sys/client/clientAdd";
    }


    /**
     * 进入添加账号界面
     * @return
     */
    @RequestMapping("addAccount/{id}")
    @RequiresPermissions({"client:edit"})
    public String addAccount(@PathVariable String id , ModelMap model) {
        model.addAttribute("clientno", id);
        return "sys/client/clientAccountAdd";
    }

    /**
     * 查询逻辑
     *
     * @param pageNumber
     * @param pageSize
     * @param client
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public PageQuery<SysClient> query(@RequestParam(defaultValue = "1") Integer pageNumber, Integer pageSize, SysClient client, @CurrentUser SysUser user)
    {
        PageQuery<SysClient> query = new PageQuery<SysClient>();
        query.setPageNumber(pageNumber);
        if (pageSize != null) {
            query.setPageSize(pageSize);
        }
        query.setParas(client);
        query = sysClientService.queryClient(query,user);
        return query;
    }
    /**
     * 查询逻辑
     *
     * @param pageNumber
     * @param pageSize
     * @param client
     * @return
     */
    @RequestMapping("queryGx")
    @ResponseBody
    public PageQuery<SysClient> queryGx(@RequestParam(defaultValue = "1") Integer pageNumber, Integer pageSize, SysClient client)
    {
        PageQuery<SysClient> query = new PageQuery<SysClient>();
        query.setPageNumber(pageNumber);
        if (pageSize != null) {
            query.setPageSize(pageSize);
        }
        query.setParas(client);
        query = sysClientService.queryClientGx(query);
        return query;
    }


    /**
     * 保存数据逻辑
     * @param sysClient
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions({"client:edit"})
    public Integer save(SysClient sysClient, @CurrentUser SysUser user) {
        sysClientService.save(sysClient,user,filePath);
        return 1;
    }

    /**
     * 保存账号数据逻辑
     * @param sysClientAccount
     * @return
     */
    @RequestMapping("saveAccount")
    @ResponseBody
    @RequiresPermissions({"client:edit"})
    public Integer saveAccount(SysClientAccount sysClientAccount) {
        sysClientAccountService.saveAccount(sysClientAccount);
        return 1;
    }

    @RequiresPermissions({"client:edit"})
    @RequestMapping("deleteAll")
    @ResponseBody
    public Integer deleteAll(String ids) {
        if(ids==null) {
            return 0;
        }
        sysClientService.deleteAll(ids);
        return 1;
    }

    /**
     * 进入修改界面
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions({"client:edit"})
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Integer id , ModelMap model) {
        SysClient sysClient = sysClientService.findById(id);
        model.addAttribute("sysClient", sysClient);
        return "sys/client/clientEdit";
    }


    /**
     * 修改数据逻辑
     * @param sysClient
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions({"client:edit"})
    public Integer update(SysClient sysClient, @CurrentUser SysUser user) {
        sysClientService.update(sysClient,user);
        return 1;
    }


    /**
     * 进入修改界面
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions({"client:edit"})
    @RequestMapping("editlist/{id}")
    public String editlist(@PathVariable Integer id , ModelMap model) {
        PageQuery<SysClientAccount> query = new PageQuery<SysClientAccount>();
        SysClient sysClient = sysClientService.findById(id);
        model.addAttribute("sysClient", sysClient);
        return "sys/client/clientAccount";
    }

    /**
     * 进入修改界面
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions({"client:edit"})
    @RequestMapping("editlistGx/{id}")
    public String editlistGx(@PathVariable Integer id , ModelMap model) {
        PageQuery<SysClientAccount> query = new PageQuery<SysClientAccount>();
        SysClient sysClient = sysClientService.findById(id);
        model.addAttribute("sysClient", sysClient);
        return "sys/client/clientAccountGx";
    }


    /**
     * 查询逻辑
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequiresPermissions({"client:edit"})
    @RequestMapping("queryAccount/{id}")
    @ResponseBody
    public PageQuery<SysClientAccount> queryAccount(@RequestParam(defaultValue = "1") Integer pageNumber, Integer pageSize, SysClientAccount sysClientAccount, @PathVariable String id)
    {
        PageQuery<SysClientAccount> query = new PageQuery<SysClientAccount>();
        sysClientAccount.setClientno(Integer.parseInt(id));
        query.setPageNumber(pageNumber);
        if (pageSize != null) {
            query.setPageSize(pageSize);
        }
        query.setParas(sysClientAccount);
        query = sysClientAccountService.queryClientAccount(query);

        return query;
    }


    /**
     * 进入修改账号界面
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions({"client:edit"})
    @RequestMapping("editAccount/{id}")
    public String editAccount(@PathVariable Integer id , ModelMap model) {
        SysClientAccount sysClientAccount = sysClientAccountService.findById(id);
        model.addAttribute("sysClientAccount", sysClientAccount);
        return "sys/client/clientAccountEdit";
    }


    /**
     * 修改数据逻辑
     * @param sysClientAccount
     * @return
     */
    @RequestMapping("updateAccount")
    @ResponseBody
    @RequiresPermissions({"client:edit"})
    public Integer updateAccount(SysClientAccount sysClientAccount) {
        sysClientAccountService.update(sysClientAccount);
        return 1;
    }

    @RequiresPermissions({"client:edit"})
    @RequestMapping("deleteAccount")
    @ResponseBody
    public Integer deleteAccount(String id) {
        if(id==null) {
            return 0;
        }
        sysClientAccountService.deleteAcoount(id);
        return 1;
    }





}
